<?php

class OrdersController extends AppController {

    var $name = 'Orders';

    function index() {
        $this->Order->recursive = 0;
        $this->set('orders', $this->paginate());
    }

    function view($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid order', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->set('order', $this->Order->read(null, $id));
        $this->loadModel('OrderProduct');
        $orderProducts = $this->OrderProduct->find('all', array('conditions' => array('OrderProduct.order_id' => $id)));
        $this->set('orderProducts', $orderProducts);
    }

    function add() {
        if (!empty($this->data)) {
            $this->Order->create();
            if ($this->Order->save($this->data)) {
                $this->Session->setFlash(__('Η αποθηκεύση έγινε επιτυχώς.', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('Η αποθηκεύση απέτυχε. Παρακαλώ ξαναπροσπαθήστε.', true));
            }
        }
        $statuses = $this->Order->Status->find('list');
        $users = $this->Order->User->find('list');
        $this->set(compact('statuses', 'users'));
    }

    function edit($id = null) {
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid order', true));
            $this->redirect(array('action' => 'index'));
        }
        if (!empty($this->data)) {
            if ($this->Order->save($this->data)) {
                $this->Session->setFlash(__('Η αποθηκεύση έγινε επιτυχώς.', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('Η αποθηκεύση απέτυχε. Παρακαλώ ξαναπροσπαθήστε.', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->Order->read(null, $id);
        }
        $this->loadModel('OrderProduct');
        $orderProducts = $this->OrderProduct->find('all', array('conditions' => array('OrderProduct.order_id' => $id)));
        $this->set('orderProducts', $orderProducts);
        $statuses = $this->Order->Status->find('list');
        $users = $this->Order->User->find('list');
        $this->set(compact('statuses', 'users'));
        $products =  $this->OrderProduct->Product->find('list');
        $this->set(compact('products'));
        $this->set('order_id' , $id);
    }

    function delete($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid id for order', true));
            $this->redirect(array('action' => 'index'));
        }
        if ($this->Order->delete($id)) {
            $this->Session->setFlash(__('Η διαγραφή έγινε επιτυχώς.', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Η διαγραφή απέτυχε.', true));
        $this->redirect(array('action' => 'index'));
    }

    function checkout() {
        if (!empty($this->data)) {
            $this->Order->create();
            if ($this->Order->saveAll($this->data, array('validate'=>'first'))) {
                $this->Session->setFlash(__('Η παραγγελία ολοκληρώθηκε.', true));
                $this->Session->write('cart', array());
                $this->redirect('/');
            } else {
                $this->Session->setFlash(__('Η αποθηκεύση απέτυχε. Παρακαλώ ξαναπροσπαθήστε.', true));
            }
        }
        if (!$this->Session->check('cart')) {
            $this->redirect('/');
        } else {
            $cart = $this->Session->read('cart');
            $result = array();
            $this->loadModel('Product');
            $ordered_products = array();
            foreach ($cart as $product_id => $quantity) {
                $product = $this->Product->find('first', array('conditions' => array('Product.id' => $product_id)));
                array_push($ordered_products, $product);
                array_push($result, array(
                    'id' => $product['Product']['id'],
                    'category' => $product['Category']['name'],
                    'name' => $product['Product']['name'],
                    'price' => $product['Product']['price'],
                    'quantity' => $quantity
                ));
            }
            $this->set('ordered_products', $ordered_products);
            $this->set('result', $result);
            $this->set('user', $this->Auth->user());
            $statuses = $this->Order->Status->find('list');
            $users = $this->Order->User->find('list');
            $products = $this->Product->find('list');
            $this->set(compact('statuses', 'users', 'products'));
        }
    }

}
